<?php

namespace app\app\controller\order;

use app\app\controller\AppBase;
use app\app\model\order\GoodsOrderModel;
use app\app\validate\order\GoodsOrderValidate;

class GoodsOrder extends AppBase
{
    protected $noNeedLogin = ['autoCancelOrder', 'info', 'goodsListInfo'];


    public function testSuccess()
    {
        $order_no = input('post.order_no');
        if (empty($order_no)) {
            $this->error('Order number not exist');
        }

        $orderModel = new GoodsOrderModel();
    }


    /** 订单创建
     * Notes:
     * User: AerYYY
     * Date: 7/2/25
     */
    public function create()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('create')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $order_no = $goodsOrderModel->createOrder($this->params);

        if ($order_no === false) {
            $this->error($goodsOrderModel->getError());
        }

        $this->success('success', ['order_no' => $order_no]);
    }


    /**
     * Notes: 订单修改
     * User: chenLei
     * Date: 8/7/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('edit')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $order_no = $goodsOrderModel->edit($this->params);

        if ($order_no === false) {
            $this->error($goodsOrderModel->getError());
        }

        $this->success('success', ['order_no' => $order_no]);

    }

    /**
     * Notes: 商品详情
     * User: chenLei
     * Date: 8/5/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('info')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->info($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }

        $this->success($res);
    }


    /**
     * Notes: 现金支付
     * User: chenLei
     * Date: 8/5/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cashPayOrder()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('cashPayOrder')->check($this->params)) {
            $this->error($validate->getError());
        }

        $this->params['pay_type'] = GoodsOrderModel::PAY_TYPE_CASH;
        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->successOrder($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }

        $this->success();
    }

    /**
     * Notes: TNT支付
     * User: chenLei
     * Date: 8/13/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tntPayOrder()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('tntPayOrder')->check($this->params)) {
            $this->error($validate->getError());
        }

        $this->params['pay_type'] = GoodsOrderModel::PAY_TYPE_TNG;
        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->successOrder($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }

        $this->success();
    }

    /**
     * Notes: 自动取消订单
     * User: chenLei
     * Date: 8/6/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function autoCancelOrder()
    {
        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->autoCancelOrder();
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success($res);
    }


    /**
     * Notes: 取消订单
     * User: chenLei
     * Date: 8/5/25
     */
    public function cancelOrder()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('cancelOrder')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->cancelOrder($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success();
    }

    /**
     * Notes: 订单列表
     * User: chenLei
     * Date: 8/6/25
     */
    public function list()
    {
        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->list($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success($res);
    }

    /**
     * Notes: 清桌
     * User: chenLei
     * Date: 8/7/25
     */
    public function clearTable()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('clearTable')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->clearTable($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success();

    }


    /**
     * Notes: 根据桌号获取订单详情
     * User: chenLei
     * Date: 8/7/25
     */
    public function boardInfo()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('boardInfo')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->boardInfo($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success($res);
    }


    /**
     * Notes: 更新订单 （再原有基础上加商品）
     * User: chenLei
     * Date: 8/13/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateOrder()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('updateOrder')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->updateOrder($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success();

    }

    /**
     * Notes: 分屏获取商品信息
     * User: chenLei
     * Date: 8/17/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function goodsListInfo()
    {
        $validate = new GoodsOrderValidate();
        if (!$validate->scene('goodsListInfo')->check($this->params)) {
            $this->error($validate->getError());
        }

        $goodsOrderModel = new GoodsOrderModel();
        $res = $goodsOrderModel->goodsListInfo($this->params);
        if ($res === false) {
            $this->error($goodsOrderModel->getError());
        }
        $this->success($res);


    }
}